﻿using BusinessResource;
using Memcached.ClientLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BusinessCache
{
    public class MemcacheFacade
    {
        private static SockIOPool _pool;
        private MemcachedClient _client;

        static MemcacheFacade()
        {
            _pool = SockIOPool.GetInstance();
            _pool.SetServers(ServerConfig.Singleton.CacheUrl.Split(','));
            _pool.InitConnections = 3;
            _pool.MinConnections = 3;
            _pool.MaxConnections = 5;
            _pool.SocketConnectTimeout = 1000;
            _pool.SocketTimeout = 3000;
            _pool.MaintenanceSleep = 30;
            _pool.Failover = true;
            _pool.Nagle = false;
            _pool.Initialize();
        }

        private MemcachedClient Client
        {
            get
            {
                if(_client == null)
                {
                    _client = new MemcachedClient();
                    _client.EnableCompression = false;
                }
                return _client;
            }
        }

        public object this[string key]
        {
            get
            {
                return Client.Get(key);
            }
        }

        public void Add(string key, object value, DateTime dateTime)
        {
            Client.Add(key, value, dateTime);
        }

        public void Set(string key, object vlaue, DateTime dateTime)
        {
            Client.Set(key, vlaue, dateTime);
        }

        public bool Contains(string key)
        {
            return Client.KeyExists(key);
        }
    }
}
